package org.xserver.component.handler;

import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.ssl.SslBufferPool;
import org.jboss.netty.handler.ssl.SslHandler;
import org.jboss.netty.util.Timer;
import org.xserver.common.bean.BaseResponse;
import org.xserver.component.core.XServerHttpResponse;
import org.xserver.component.json.JsonManager;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLHandshakeException;
import java.util.concurrent.Executor;

/**
 * @author bin
 * @since 2016-04-05
 */
@Deprecated
public class XServerSslHandler extends SslHandler {
	public XServerSslHandler(SSLEngine engine) {
		super(engine);
	}

	public XServerSslHandler(SSLEngine engine, SslBufferPool bufferPool) {
		super(engine, bufferPool);
	}

	public XServerSslHandler(SSLEngine engine, boolean startTls) {
		super(engine, startTls);
	}

	public XServerSslHandler(SSLEngine engine, SslBufferPool bufferPool,
							 boolean startTls) {
		super(engine, bufferPool, startTls);
	}

	public XServerSslHandler(SSLEngine engine, Executor delegatedTaskExecutor) {
		super(engine, delegatedTaskExecutor);
	}

	public XServerSslHandler(SSLEngine engine, SslBufferPool bufferPool,
							 Executor delegatedTaskExecutor) {
		super(engine, bufferPool, delegatedTaskExecutor);
	}

	public XServerSslHandler(SSLEngine engine, boolean startTls, Executor delegatedTaskExecutor) {
		super(engine, startTls, delegatedTaskExecutor);
	}

	public XServerSslHandler(SSLEngine engine, SslBufferPool bufferPool, boolean startTls,
							 Executor delegatedTaskExecutor) {
		super(engine, bufferPool, startTls, delegatedTaskExecutor);
	}

	public XServerSslHandler(SSLEngine engine, SslBufferPool bufferPool, boolean startTls,
							 Executor delegatedTaskExecutor, Timer timer, long handshakeTimeoutInMillis) {
		super(engine, bufferPool, startTls, delegatedTaskExecutor, timer, handshakeTimeoutInMillis);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
		super.exceptionCaught(ctx, e);
		BaseResponse baseResponse = new BaseResponse();
		XServerHttpResponse xServerHttpResponse = new XServerHttpResponse();
		xServerHttpResponse.setHeaderIfEmpty("Access-Control-Allow-Origin", "*");
		//如果存在自定义的header参数，需要在此处添加，逗号分隔
		xServerHttpResponse.setHeaderIfEmpty("Access-Control-Allow-Headers",
				"Origin, No-Cache, X-Requested-With, "
				+ "If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, "
				+ "Content-Type, X-E4M-With");
		xServerHttpResponse.setHeaderIfEmpty("Access-Control-Allow-Methods",
				"GET, POST, OPTIONS");

		if (e.getCause() instanceof SSLHandshakeException) {
			xServerHttpResponse.setStatus(HttpResponseStatus.UNAUTHORIZED);
			baseResponse.setErrcode(String.valueOf(HttpResponseStatus.UNAUTHORIZED.getCode()));
			baseResponse.setDescription(HttpResponseStatus.UNAUTHORIZED.getReasonPhrase());
			baseResponse.setResult(e.getCause());
			xServerHttpResponse.setContentString(JsonManager.json(baseResponse));
			ctx.getChannel().write(xServerHttpResponse)
					.addListener(ChannelFutureListener.CLOSE);
		}
	}
}
